﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data.SqlClient;
using System.Configuration;
using Dapper;

namespace DAL
{
   public  class UserMenuDAL
    {
        public static readonly string SqlConnString = ConfigurationManager.ConnectionStrings["ELCO_ConnectionString"].ConnectionString;

       /// <summary>
       /// 根据登录用户获取左侧菜单
       /// </summary>
       /// <param name="user"></param>
       /// <returns></returns>
        public static List<UserMenu> GetUserMenuList(string user)
        {

            using (var conn = new SqlConnection(SqlConnString))
            {

                string menusql = "select MenuNo,MenuName,MenuAddr,ParentNo,Image from User_LimitInfo where UserNo='" + user + "' order by MenuNo";
                return conn.Query<UserMenu>(menusql).ToList();

            }
        }

        /// <summary>
        /// 获取权限树
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static List<MenuTree> GetLimitTree()
        {

            using (var conn = new SqlConnection(SqlConnString))
            {

                string menusql = "select MenuNo id,MenuName name,MenuAddr,ParentNo pId,MenuTag,Image img from UserM_Menu  order by MenuNo ";
                string operatesql = "select OperateNo id,MenuName name,'' MenuAddr,MenuNo  pId from UserM_OperateInfo order by MenuNo ";

                List<MenuTree> menutree = conn.Query<MenuTree>(menusql).ToList();
                List<MenuTree> operatetree = conn.Query<MenuTree>(operatesql).ToList();

                for (int i = 0; i < menutree.Count; i++ )
                {
                    List<MenuTree> menu = operatetree.Where(p => p.pId == menutree[i].id).ToList();
                    foreach(var m in menu)
                    {
                        menutree.Add(m);
                    }
                }


                for (int i = 0; i < menutree.Count(); i++ )
                {
                    menutree[i].open = true;
                }
                return menutree;

            }
        }

       /// <summary>
        /// 新增角色页面  当是客户登录 构造权限树
       /// </summary>
       /// <param name="CustomerName"></param>
       /// <returns></returns>
        public static List<MenuTree> initRoleLimitByCustomer(string CustomerName)
        {

            using (var conn = new SqlConnection(SqlConnString))
            {

                string menusql = "select MenuNo id,MenuName name,MenuAddr,ParentNo pId,MenuTag,Image img from UserM_Menu where MenuNo in (select MenuNo FROM UserM_RoleLimit WHERE RoleID = (SELECT TOP(1) ID FROM UserM_RoleInfo WHERE CustomName = '" + CustomerName + "')) order by MenuNo ";
                string operatesql = "select OperateNo id,MenuName name,'' MenuAddr,MenuNo  pId from UserM_OperateInfo where MenuNo in (select MenuNo FROM UserM_RoleLimit WHERE RoleID = (SELECT TOP(1) ID FROM UserM_RoleInfo WHERE CustomName = '" + CustomerName + "')) order by MenuNo ";

                List<MenuTree> menutree = conn.Query<MenuTree>(menusql).ToList();
                List<MenuTree> operatetree = conn.Query<MenuTree>(operatesql).ToList();

                for (int i = 0; i < menutree.Count; i++)
                {
                    List<MenuTree> menu = operatetree.Where(p => p.pId == menutree[i].id).ToList();
                    foreach (var m in menu)
                    {
                        menutree.Add(m);
                    }
                }


                for (int i = 0; i < menutree.Count(); i++)
                {
                    menutree[i].open = true;
                }
                return menutree;

            }
        }
    }
}
